From 802eea628fb77fed305de555995cf323174b0394 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 22 Sep 2026 11:23:19 -0700 Subject: invoice: a reusable bolt12 offer has no terminal paid state A bolt12 offer is a standing payment code. CLN derives the same offer_id from the same (amount, description), so re-creating one hands back the original record, whose `received` has accumulated over the offer's whole lifetime. The invoice layout read that as "this invoice was paid" and redirected to the success screen, so reopening a long-lived offer never showed the QR and left no way to copy the lno. Treat a payment as celebratory only when it arrives while the page is open: the socket handler now passes the amount of that payment as `?paid=`, and the layout uses it in place of the lifetime total for reusable offers. Single-use invoices keep the existing logic, so a partly-paid invoice still stays on the QR rather than celebrating early. The success screen showed lifetime `received` for the same reason, which misreported a single payment to a long-lived offer; it now renders the amount the socket reported, which is already net of tip. Co-Authored-By: Claude Opus 5 (1M context) --- src/routes/(app)/invoice/[id]/+layout.server.ts | 34 +++++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'src/routes/(app)/invoice/[id]/+layout.server.ts') diff --git a/src/routes/(app)/invoice/[id]/+layout.server.ts b/src/routes/(app)/invoice/[id]/+layout.server.ts index ab41dddc..faabb753 100644 --- a/src/routes/(app)/invoice/[id]/+layout.server.ts +++ b/src/routes/(app)/invoice/[id]/+layout.server.ts @@ -8,6 +8,10 @@ export async function load({ cookies, depends, params, url, parent }) { let { subject, user } = await parent(); const { id } = params; let invoice; + // Amount of a payment that arrived while the page was open, set by the + // socket handler. Only meaningful for reusable (bolt12) offers, whose + // `received` is a lifetime total rather than this payment. + let justPaid: string | null = null; if (id) { invoice = await get(`/invoice/${id}`); @@ -15,11 +19,31 @@ export async function load({ cookies, depends, params, url, parent }) { let { amount, pending, received } = invoice; amount = parseInt(amount); - const paid = - (!amount && (pending || received)) || - (amount > 0 && (pending >= amount || received >= amount)); + // A bolt12 offer is a standing, REUSABLE payment code: CLN derives the same + // offer_id from the same (amount, description), so re-creating one hands back + // the original record with `received` accumulated over its entire lifetime. + // Treating that as "paid" bounces the user straight to the success screen so + // they never get to see the QR or copy the lno — and a long-lived offer that + // takes regular payouts is effectively unusable, since its last payment may + // be hours or months old. A standing offer has no terminal paid state, so + // only a payment arriving live counts — the socket handler signals that + // with `?paid=`. + const reusable = invoice.type === "bolt12"; + justPaid = url.searchParams.get("paid"); + + const paid = reusable + ? !!justPaid + : (!amount && (pending || received)) || + (amount > 0 && (pending >= amount || received >= amount)); + if (paid && !url.pathname.endsWith("paid")) { - redirect(307, `/invoice/${id}/paid` + (options ? "?options=true" : "")); + const q = new URLSearchParams(); + if (options) q.set("options", "true"); + // Carry the amount of THIS payment through; the success screen would + // otherwise render lifetime `received` for a reusable offer. + if (justPaid) q.set("paid", justPaid); + const qs = q.toString(); + redirect(307, `/invoice/${id}/paid${qs ? `?${qs}` : ""}`); } subject = invoice.user; @@ -39,5 +63,5 @@ export async function load({ cookies, depends, params, url, parent }) { } const theme = cookies.get("theme") || "light"; - return { id, invoice, subject, user, token, theme }; + return { id, invoice, subject, user, token, theme, justPaid }; } -- cgit v1.2.3